package com.xianbeiying.app.ckgl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.ChoiceGhAdapter;
import com.xianbeiying.app.ckgl.bean.DevicesBean;
import com.xianbeiying.app.ckgl.bean.GzQdBean;
import com.xianbeiying.app.ckgl.bean.KqWdBean;
import com.xianbeiying.app.ckgl.bean.TrSdBean;
import com.xianbeiying.app.ckgl.bean.TrwdBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.GetModel;
import com.xianbeiying.app.ckgl.util.DataUtil;
import com.xianbeiying.app.ckgl.util.StringUtil;
import com.xianbeiying.app.ckgl.widght.SlideShowView;
import com.xianbeiying.app.ckgl.widght.dashview.DashboardView;
import com.xianbeiying.app.ckgl.widght.dashview.HighlightCR;
import com.xianbeiying.app.ckgl.widght.popwindow.CenterPopWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * 实时信息
 */
public class RealTimessInfoActivity extends BaseActivity implements View.OnClickListener, HttpCallBackUtil {
    private ImageView iv_title_left;
    private TextView tv_title;
    private ImageView iv_info_img;
    private ImageView iv_info_add, iv_info_jian;
    private TextView tv_info_shebei; //设备
    private TextView tv_info_number; //设备号
    private TextView tv_info_time; //时间
    private CenterPopWindow centerPopWindow;
    private GetModel getModel;
    private int selectId = 0;
    private TextView tv_dapeng;
    private DashboardView kqwd_view,kqsd_view;//,trwd_view,trsd_view,gzqd_view,eyht_view,ystj_view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_real_timess_info);
        initView();
    }

    private void initView() {
        getModel = new GetModel(this);
        getModel.addResponseListener(this);
        iv_info_img = (ImageView) findViewById(R.id.iv_info_img);
        iv_info_add = (ImageView) findViewById(R.id.iv_info_add);
//        menu=(ShrinkMenu) findViewById(R.id.menu);
        iv_info_jian = (ImageView) findViewById(R.id.iv_info_jian);
        tv_info_shebei = (TextView) findViewById(R.id.tv_info_shebei);
        tv_info_number = (TextView) findViewById(R.id.tv_info_number);
        tv_info_time = (TextView) findViewById(R.id.tv_info_time);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_dapeng=(TextView) findViewById(R.id.tv_dapeng);
        kqwd_view=(DashboardView)findViewById(R.id.kqwd_view);
        kqsd_view=(DashboardView)findViewById(R.id.kqsd_view);
//        trwd_view=(DashboardView)findViewById(R.id.trwd_view);
//        trsd_view=(DashboardView)findViewById(R.id.trsd_view);
//        gzqd_view=(DashboardView)findViewById(R.id.gzqd_view);
//        eyht_view=(DashboardView)findViewById(R.id.eyht_view);
//        ystj_view=(DashboardView)findViewById(R.id.ystj_view);
        tv_title.setText("实时信息");
        initData();
        initSetView();
        intEvent();
    }

    private void initSetView() {
        List<HighlightCR> highlight2 = new ArrayList<>();
        highlight2.add(new HighlightCR(170, 50, ContextCompat.getColor(this,R.color.rinfo_lv)));
        highlight2.add(new HighlightCR(210, 140, ContextCompat.getColor(this,R.color.rinfo_lan)));
        highlight2.add(new HighlightCR(330, 50,ContextCompat.getColor(this,R.color.rinfo_h)));
        kqwd_view.setStripeHighlightColorAndRange(highlight2);
        kqsd_view.setStripeHighlightColorAndRange(highlight2);
//        trwd_view.setStripeHighlightColorAndRange(highlight2);
//        trsd_view.setStripeHighlightColorAndRange(highlight2);
//        gzqd_view.setStripeHighlightColorAndRange(highlight2);
//        eyht_view.setStripeHighlightColorAndRange(highlight2);
//        ystj_view.setStripeHighlightColorAndRange(highlight2);
    }
    private void initData() {
        if (MyApplication.devicesBean != null) {
            devicesBean = MyApplication.devicesBean;
            getData();
            tv_info_shebei.setText(devicesBean.getGREENHOUSE().get(selectId).getGreenhouseName());
            tv_dapeng.setText(devicesBean.getGREENHOUSE().get(selectId).getGreenhouseName()+"实时信息数据");
            getModel.GET_AIRTEMPHUM(devicesBean.getGREENHOUSE().get(selectId).getGreenhouseId(), true);
        } else {
            getModel.QUWEYDEVS(true);
        }
        tv_info_time.setText(DataUtil.dateTime()+"  "+DataUtil.tiemStr());

    }

    private void intEvent() {
        iv_info_add.setOnClickListener(this);
        iv_info_jian.setOnClickListener(this);
        iv_title_left.setOnClickListener(this);
        tv_info_shebei.setOnClickListener(this);
        iv_info_img.setOnClickListener(this);
    }

    private ListView listView;
    private ChoiceGhAdapter choiceGhAdapter;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_info_img:
            //放大
            case R.id.iv_info_add:
            //缩小
            case R.id.iv_info_jian:
                startActivity(new Intent().setClass(RealTimessInfoActivity.this,LookBigPictureActivity.class).putExtra("imgUrl", SlideShowView.imageUrls.get(SlideShowView.selectId)));
                break;
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.tv_info_shebei:
                centerPopWindow = new CenterPopWindow(this, R.layout.pop_window_info);
                centerPopWindow.setText(R.id.tv_name, "选择大棚");
                listView = centerPopWindow.getView(R.id.list_city);
                centerPopWindow.setTextOnClickListener(R.id.ly_pop_window_city, this);
                if (date != null && choiceGhAdapter == null) {
                    choiceGhAdapter = new ChoiceGhAdapter(RealTimessInfoActivity.this, date);
                    choiceGhAdapter.setArrayList(StringUtil.getArryList(date));
                }
                listView.setAdapter(choiceGhAdapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        int size = choiceGhAdapter.getArrayList().size();
                        for (int i = 0; i < size; i++) {
                            if (i == position) {
                                selectId = position;
                                choiceGhAdapter.getArrayList().set(position, true);
                                getModel.GET_AIRTEMPHUM(devicesBean.getGREENHOUSE().get(i).getGreenhouseId(), true);

                            } else {
                                choiceGhAdapter.getArrayList().set(i, false);
                            }
                        }
                        choiceGhAdapter.notifyDataSetChanged();
                        tv_info_shebei.setText(devicesBean.getGREENHOUSE().get(selectId).getGreenhouseName());
                        tv_dapeng.setText(devicesBean.getGREENHOUSE().get(selectId).getGreenhouseName()+"实时信息数据");
                        tv_info_number.setText((position + 1) + "");
                        centerPopWindow.dismiss();

                    }
                });
                break;
            case R.id.ly_pop_window_city:
                centerPopWindow.dismiss();
                break;
        }
    }

    //网络返回值
    private DevicesBean devicesBean;
    private String[] date;
    private KqWdBean kqWdBean;
    private TrSdBean trSdBean;
    private TrwdBean trwdBean;
    private GzQdBean gzQdBean;
    @Override
    public void onCallBackData(String url, Object content) {
        if (url.equals(UrlBean.QUWEYDEVS)) {
            if (content != null) {
                devicesBean = JSON.parseObject(new String((byte[]) content), DevicesBean.class);
                MyApplication.devicesBean = devicesBean;
                getData();
                tv_info_shebei.setText(devicesBean.getGREENHOUSE().get(selectId).getGreenhouseName());
                tv_dapeng.setText(devicesBean.getGREENHOUSE().get(selectId).getGreenhouseName()+"实时信息数据");
                getModel.GET_AIRTEMPHUM(devicesBean.getGREENHOUSE().get(selectId).getGreenhouseId(), false);
            }
        }
        if (url.equals("空气温湿度")) {
            if (content != null) {
                kqWdBean = JSON.parseObject(new String((byte[]) content), KqWdBean.class);
                kqwd_view.setRealTimeValue( kqWdBean.getV().getTEMPERATURE(),true,500);
                kqsd_view.setRealTimeValue(kqWdBean.getV().getHUMIDITY(),true,500);
            }
//            getModel.GET_AIRTEMPHUM(devicesBean.getGREENHOUSE().get(selectId).getGreenhouseId(), false);
//            getModel.GET_SOILHUM(devicesBean.getGREENHOUSE().get(selectId).getGreenhouseId(), false);

        }
        if (url.equals("土壤湿度")) {
            if (content != null) {
                trSdBean = JSON.parseObject(new String((byte[]) content), TrSdBean.class);
            }

            getModel.GET_SOILTEMP(devicesBean.getGREENHOUSE().get(selectId).getGreenhouseId(), false);
        }
        if (url.equals("土壤温度")) {
            if (content != null) {
                trwdBean = JSON.parseObject(new String((byte[]) content), TrwdBean.class);
            }
            getModel.GET_BEAMSTRENGTH(devicesBean.getGREENHOUSE().get(selectId).getGreenhouseId(), true);

        }
        if (url.equals("光照强度")) {
            if (content != null){
                gzQdBean = JSON.parseObject(new String((byte[]) content), GzQdBean.class);
//                xdata = new int[7];
//                xdata[0] = kqWdBean.getV().getTEMPERATURE();
//                xdata[1] = kqWdBean.getV().getHUMIDITY();
//                xdata[2] = trSdBean.getV().getSOILHUMIDITY();
//                xdata[3] = trwdBean.getV().getSOILTEMP();
//                xdata[4] = gzQdBean.getV().getBEAMSTRENGTH();
                kqwd_view.setRealTimeValue( kqWdBean.getV().getTEMPERATURE(),true,500);
                kqsd_view.setRealTimeValue(kqWdBean.getV().getHUMIDITY(),true,500);
//                trwd_view.setRealTimeValue(trSdBean.getV().getSOILHUMIDITY(),true,500);
//                trsd_view.setRealTimeValue(trwdBean.getV().getSOILTEMP(),true,500);
//                gzqd_view.setRealTimeValue(gzQdBean.getV().getBEAMSTRENGTH(),true,500);
//                eyht_view.setRealTimeValue(0,true,500);
//                ystj_view.setRealTimeValue(0,true,500);
            }


        }
    }

    private void getData() {
        int size=devicesBean.getGREENHOUSE().size();
        date = new String[size];
        for (int i = 0; i < size; i++) {
            date[i] =devicesBean.getGREENHOUSE().get(i).getGreenhouseName();
        }
    }
}
